package lang.object;

import entity.Shop;
import entity.User;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 李栋
 * @version 1.0.0
 * @className ObjectTest.java
 * @createTime 2021年07月18日 21:16:00
 */
public class ObjectTest {

    private static final Logger logger = LoggerFactory.getLogger(ObjectTest.class);


    /**
     * - 基本数据类型：`equals`不能比较基本数据类型的变量，`==`比较的是对象的内容是否相等。
     * - 引用数据类型：`equals`默认比较的是地址是否相等，当重写了`equals`方法后比较的是对象的内容是否相同，`==`比较的是所指向的对象的地址。
     * `Object`的`equals`比较的是对象的地址是否相同，`String`重写 `equals`比较的是对象的地址上的值是否相等。
     **/
    @Test
    public void objectEqual() {
        Object objectOne = "test";
        Object objectTwo = "methodTest";
        assert objectOne.equals(objectTwo);
    }

    @Test
    public void objectEqualOne() {
        Object objectOne = 3;
        Object objectTwo = 3;
        assert objectOne.equals(objectTwo);
    }


    @Test
    public void overrideEqualsHash() {
        Shop shopOne = new Shop();
        shopOne.setShopCode("001");
        shopOne.setShopName("name1");

        Shop shopTwo = new Shop();
        shopTwo.setShopCode("001");
        shopTwo.setShopName("name2");

        assert shopOne.equals(shopTwo);
    }

    @Test
    public void objectWait() throws InterruptedException {
        Shop shopOne = new Shop();
        shopOne.setShopCode("001");
        shopOne.setShopName("name1");
        shopOne.wait();
        logger.info("test!");
    }

    @Test
    public void objectCompareTest() {
        List<Shop> list = new ArrayList<>();
        Shop shop1 = new Shop("100001", "test1rqwerqwerqwe");
        Shop shop2 = new Shop("100002", "test12");
        Shop shop3 = new Shop("100003", "test1423423");
        list.add(shop1);
        list.add(shop2);
        list.add(shop3);
        Collections.sort(list);
        logger.info(String.valueOf(list));
    }


    /**
     * Java 创建对象方法
     */
    @Test
    public void createObject() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        User user = new User();
        Object user1 = Class.forName("entity.User").newInstance();
        User user2 = User.class.newInstance();
        logger.info("{} {} {}", user, user1, user2);
    }

    @Test
    public void testOne() {
        String test = "张三";
        logger.info(String.valueOf(test.hashCode()));
    }

    @Test
    public void testTwo() throws IOException, ClassNotFoundException {
        //序列化
        FileOutputStream fileOutputStream = new FileOutputStream("object.txt");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        User user1 = new User(1, "123456", "male");
        objectOutputStream.writeObject(user1);
        objectOutputStream.flush();
        objectOutputStream.close();
        //反序列化
        FileInputStream fileInputStream = new FileInputStream("object.txt");
        ObjectInputStream ois = new ObjectInputStream(fileInputStream);
        User user2 = (User) ois.readObject();
        logger.info(user2.getId() + " " + user2.getName() + " " + user2.getAddress());
    }
}
